package pers.yurwisher.rubick.oauth2.security;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import pers.yurwisher.rubick.common.user.CurrentUser;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yq
 * @date 2021年4月15日 12:29:52
 * @description Oauth2User
 * @since V1.0.0
 */
@Getter
public class OAuth2User implements UserDetails {

    private static final long serialVersionUID = -7443286142453207263L;

    private final CurrentUser user;
    /**
     * 权限
     */
    private final List<GrantedAuthority> authorities;

    public OAuth2User(CurrentUser user) {
        this.authorities = CollectionUtil.newArrayList();
        this.user = user;
    }

    @Override
    @JSONField(serialize = false)
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    @JSONField(serialize = false)
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isEnabled() {
        return user.getEnabled();
    }

}
